/**
 *  异步路由合并，过滤未授权菜单
 */
import {
	constantRoutes,
	asyncRouters,
	endRouters
} from "@/router";
import {
	getMenu
} from "@/api/auth";
import {
	SsearchMenu
} from "@/api/PublicSearch";
import Layout from "@/layout";

/**
 * 判断是否授权
 * @param routes 请求返回的授权路由数据
 * @param mapRoute 本地定义的异步路由项
 * @return number 返回授权路由项的下标
 */
function hasPermission(routes, mapRoute) {
	if (mapRoute.name) {
		return routes.findIndex((item) => {
			return item.menuRouter === mapRoute.name;
		});
	}
	return -1;
}
/**
 * 排除未授权路由
 * @param routes 请求返回的授权数据
 * @param asyncMap 本地定义的异步路由表
 */
export function filterAsyncRoutes(routes, asyncMap) {
	const res = [];
	routes.forEach((item) => {
		res.push({
			path: item.menuRouter,
			component: Layout,
			name: item.menuRouter,
			children: [{
				path: item.menuRouter,
				component: (resolve) =>
					require(["@/views" + item.menuRouter + item.menuRouter], resolve),
				name: item.menuRouter,
				meta: {
					title: item.menuName,
					icon: item.routerIcon,
					affix: true
				},
			}, ],
		});
		//   path: "/",
		//   component: Layout,
		//   name: "/home",
		//   meta: { title: "首页", icon: "el-icon-s-help" },
		//   children: [
		//     {
		//       path: "/Home",
		//       component: () => import("@/views/home/home"),
		//       name: "/Home",
		//       meta: { affix: true },
		//     },
		//   ],
		// },
		// const routesIndex = hasPermission(routes, tmp);
		// if (routesIndex !== -1) {
		//   if (tmp.children && routes[routesIndex]["_child"]) {
		//     tmp.children = filterAsyncRoutes(
		//       routes[routesIndex]["_child"],
		//       tmp.children
		//     );
		//   }
		// 加入排序
		// tmp.sort = routes[routesIndex]["sort_order"] || 1000;
		// // 更新Title
		// tmp.meta.title = routes[routIesndex]["menuName"] || tmp.meta.title;
		// console.log(tmp);
		// // 更新icon
		// tmp.meta.icon = routes[routesIndex]["routerIcon"] || tmp.meta.icon;
		// }
	});
	return res;
}

const state = {
	routes: [],
	addRoutes: [],
	isAddRoutes: false,
};

const mutations = {
	SET_ROUTES: (state, routes) => {
		state.addRoutes = routes;
		state.isAddRoutes = true;
		// 合并，排序路由
		const newRoutes = constantRoutes.concat(routes);
		state.routes = newRoutes
			.sort(function(a, b) {
				return a.sort - b.sort;
			})
			.concat(endRouters);
	},
	SET_IS_ADD_ROUTES: (state, isAddRoutes) => {
		state.isAddRoutes = isAddRoutes;
	},
};

const actions = {
	generateRoutes({
		commit
	}) {
		return new Promise((resolve, reject) => {
			let accessedRoutes = [];
			// 请求用户权限菜单
			getMenu()
				.then((response) => {
					const {
						data
					} = response;
					// console.log(response)
					if (!data) {
						resolve([]);
						return;
					}
					// 处理匹配用户菜单
					accessedRoutes = filterAsyncRoutes(data);
					// 保存用户权限菜单
					commit("SET_ROUTES", accessedRoutes);
					resolve(accessedRoutes);
				})
				.catch((error) => {
					return reject(error);
				});
		});
	},
	removeRoutes({
		commit
	}) {
		return commit("SET_IS_ADD_ROUTES", false);
	},
};

export default {
	namespaced: true,
	state,
	mutations,
	actions,
};
